package com.insurance.utils;

import com.insurance.common.Contants;
import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * User: 朱雄
 * Date: 2020/09/27 18:46
 * Description:邮箱激活
 * Version: V1.0
 */
public class EmailUtil implements Runnable{
    // 收件人邮箱
    private String receiveEmail;
    //发送内容
    private String content1;
    //接收人
    private String user;
    /**
     *
    163邮箱
     */
    private String emali="@163";
    public EmailUtil(String receiveEmail,String content,String user) {
        this.receiveEmail = receiveEmail;
        this.content1=content;
        this.user=user;
    }

    @Override
    public void run() {
        if(receiveEmail.contains(emali)){
            System.out.println("=======163邮箱=======");

            // 1.创建连接对象javax.mail.Session
            // 2.创建邮件对象 javax.mail.Message
            // 3.发送一封激活邮件
            // 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易)
            String host = "smtp.163.com";
            // 获取系统属性
            Properties properties = System.getProperties();
            // 设置邮件服务器
            properties.setProperty("mail.smtp.host", host);
            // 打开认证
            properties.setProperty("mail.smtp.auth", "true");
            try {
                // 1.获取默认session对象
                Session session = Session.getDefaultInstance(properties, new Authenticator() {
                    @Override
                    public PasswordAuthentication getPasswordAuthentication() {
                        // 发件人邮箱账号、授权码
                        return new PasswordAuthentication("zhu1758876889@163.com","NNWAMIKTQZOVNIAJ");
                    }
                });
                // 2.创建邮件对象
                Message message = new MimeMessage(session);
                // 3.设置发件人
                message.setFrom(new InternetAddress("zhu1758876889@163.com"));
                // 4.设置收件人
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(receiveEmail));
                // 5.设置邮件主题
                message.setSubject("易理赔消息");
                String content = "<html><head></head><body>" +
                        "<h1>尊敬的"+user+"您好</h1><h3>" +
                        "<h3>"+content1+"</h3></body></html>";
                message.setContent(content, "text/html;charset=UTF-8");
                // 7.发送邮件
                // 阻塞方法
                Transport.send(message);
                System.out.println("=============邮件成功发送!=============");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
        // 1.创建连接对象javax.mail.Session
        // 2.创建邮件对象 javax.mail.Message
        // 3.发送一封激活邮件
        // 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易)
        String host = "smtp.qq.com";
        // 获取系统属性
        Properties properties = System.getProperties();
        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", host);
        // 打开认证
        properties.setProperty("mail.smtp.auth", "true");
        //QQ邮箱需要下面这段代码，163邮箱不需要
        try {
            //QQ邮箱需要下面这段代码，163邮箱不需要
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);
            // 1.获取默认session对象
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    // 发件人邮箱账号、授权码
                    return new PasswordAuthentication(Contants.SEND_EMAIL_ACCOUNT, Contants.SEND_EMAIL_CODE);
                }
            });
            // 2.创建邮件对象
            Message message = new MimeMessage(session);
            // 3.设置发件人
            message.setFrom(new InternetAddress(Contants.SEND_EMAIL_ACCOUNT));
            // 4.设置收件人
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(receiveEmail));
            // 5.设置邮件主题
            message.setSubject("易理赔消息");
            String content = "<html><head></head><body>" +
                    "<h1>尊敬的"+user+"您好</h1><h3>" +
                    "<h3>"+content1+"</h3></body></html>";
            message.setContent(content, "text/html;charset=UTF-8");
            // 7.发送邮件
            // 阻塞方法
            Transport.send(message);
            System.out.println("=============邮件成功发送!=============");
        } catch (Exception e) {
            e.printStackTrace();
        }
        }
    }

}
